var e = getApp(), t = e.requirejs("core"), a = e.requirejs("foxui"), i = e.requirejs("wxParse/wxParse");

Page({
    data: {
        id: 0,
        detail: {},
        czlog_id: 0,
        card_type: 0
    },
    onLoad: function(e) {
        this.setData({
            id: e.id
        })
    },
    getDetail: function() {
        var e = this;
        t.get("sale/newcoupon/getdetail", {
            id: this.data.id
        }, function(t) {
            t.error > 0 ? wx.navigateBack() : (i.wxParse("wxParseData", "html", t.detail.desc, e, "5"), 
            e.setData({
                detail: t.detail,
                czlog_id: t.czlog_id,
                card_type: t.card_type,
                show: !0
            }));
        });
    },
    onShow: function() {
        this.getDetail();
        e.checkAuth();
    },
    receive: function(e) {
        var i = this.data.detail, s = this;
        if (this.data.buying) a.toast(s, "正在执行请稍后"); 
        else if (1 == i.canget) {
            var o = "确认使用";
            // i.money > 0 && (o += i.money + "元", i.credit > 0 && (o += "+")), i.credit > 0 && (o += i.credit + "积分"), 
            // o += i.gettypestr + "吗？", t.confirm(o, function() {
            //     s.setData({
            //         buying: !0
            //     }), t.post("sale/coupon/pay", {
            //         id: s.data.id
            //     }, function(e) {
            //         if (e.error > 0) return a.toast(s, e.message), void s.setData({
            //             buying: !1
            //         });
            //         s.setData({
            //             logid: e.logid
            //         }), e.wechat && e.wechat.success ? t.pay(e.wechat.payinfo, function(e) {
            //             "requestPayment:ok" == e.errMsg && s.payResult();
            //         }) : s.payResult(), s.setData({
            //             buying: !1
            //         });
            //     });
            // });
            if(i.money>0){
                o += i.money + "元";
            }
            o += i.gettypestr + "吗？", t.confirm(o, function() {
                s.setData({
                    buying: !0
                }), t.post("sale/newcoupon/pay", {
                    id: s.data.id,
                    czlog_id: s.data.czlog_id,
                    card_type: s.data.card_type
                }, function(e) {
                    console.log(e);
                    if (e.error > 0) return a.toast(s, e.message), void s.setData({
                        buying: !1
                    });
                    if(e.error==0){
                        s.setData({
                            logid: e.logid
                        });
                        if(e.credit && e.credit.success){
                            s.payResult('credit');
                        }else if(e.merchcredit && e.merchcredit.success){
                            s.payResult('merchcredit');
                        }else{
                            t.pay(e.wechat.payinfo, function(e) {
                                "requestPayment:ok" == e.errMsg && s.payResult('wechat');
                            });
                        }
                    }else{
                        a.toast(s, e.message);
                        s.setData({
                            buying: !1
                        });
                    }
                });
            });
        } else if (-2 == i.canget) {
            wx.showModal({
                title: "提示",
                content: i.getstr,
                success: function (sm) {
                    if (sm.confirm) {
                        wx.navigateTo({
                          url: "/mx/recharge/index?merchid="+i.merchid+"&czmoney="+i.czmoney,
                        })
                    } else if (sm.cancel) {
                        console.log(2)
                    }
                }
            })
        }else a.toast(s, i.getstr);
    },
    payResult: function(paytype) {
        var e = this;
        t.get("sale/newcoupon/payresult", {
            logid: this.data.logid,
            paytype: paytype
        }, function(i) {
            if (i.error > 0) {
                a.toast(e, i.message); 
            } else {
                wx.redirectTo({
                    url: "/pages/sale/coupon/my/showcoupons2/index?id=" + i.dataid
                });
            }
        });
    }
});